package com.lvmama.rhino.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.rhino.enums.TopicPVTypeEnum;
import com.lvmama.rhino.mapper.PositionAnalysisMapper;
import com.lvmama.rhino.pojo.PitPV;
import com.lvmama.rhino.pojo.PositionAnalysis;
import com.lvmama.rhino.pojo.TopicFunnelDataQuery;
import com.lvmama.rhino.pojo.TopicFunnelVO;
import com.lvmama.rhino.pojo.funnel.SpecialTopicFunnelData;
import com.lvmama.rhino.service.PositionAnalysisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: PositionAnalysisServiceImpl
 * @Description: 专题与坑位相关的接口实现方法
 * @author Administrator
 * @date 2017年5月19日 下午5:13:38
 */
@Service("positionAnalysisService")
public class PositionAnalysisServiceImpl implements PositionAnalysisService {
	@Autowired
	private PositionAnalysisMapper positionAnalysisMapper;

	// 分页获取专题列表
	@Override
	public PageInfo<PositionAnalysis> getTopicList(PositionAnalysis sc) {
		PageHelper.startPage(sc.getCurrentPage(), sc.getPage_size());
		List<PositionAnalysis> positionAnalysis = positionAnalysisMapper
				.getAllList(sc);
		PageInfo<PositionAnalysis> page = new PageInfo<PositionAnalysis>(
				positionAnalysis);
		return page;
	}

	// 获取坑位列表
	@Override
	public PageInfo<PositionAnalysis> getPitList(PositionAnalysis positionVO) {
		PageHelper.startPage(positionVO.getCurrentPage(), positionVO.getPage_size());
		List<PositionAnalysis> positionAnalysis = positionAnalysisMapper.queryListByPit(positionVO);
		PageInfo<PositionAnalysis> page = new PageInfo<PositionAnalysis>(
				positionAnalysis);
		return page;
	}

	public List<PositionAnalysis> getPitListByCategory(
			PositionAnalysis positionVO) {
		if (StringUtils.isEmpty(positionVO.getPlatform())) {
			return positionAnalysisMapper.queryThemeByCategory(positionVO);
		} else {
			return positionAnalysisMapper.queryListByCategory(positionVO);
		}
	}


	@Override
	public List<PitPV> getPitPVByCategory(PositionAnalysis positionVO) {
		return positionAnalysisMapper.getPitPVByCategory(positionVO);
	}

	@Override
	public List<PitPV> getAllPitPV(PositionAnalysis positionVO) {
		return positionAnalysisMapper.getAllPitPV(positionVO);
	}

	@Override
	public PageInfo<PitPV> getTopicPVByCode(PositionAnalysis positionVO) {
		PageHelper.startPage(positionVO.getCurrentPage(), positionVO.getPage_size());
		List<PitPV> pitPVS = new ArrayList<PitPV>();
		if(positionVO.getName()== null){
			pitPVS = positionAnalysisMapper.getTopicPVByCode(positionVO);
		}else{
			pitPVS = positionAnalysisMapper.getTopicAllPVByCode(positionVO);
		}
		PageInfo<PitPV> page = new PageInfo<PitPV>(pitPVS);
		return page;
	}

	public List<PositionAnalysis> getPitListByDate(PositionAnalysis positionVO) {
		if (StringUtils.isEmpty(positionVO.getPlatform())) {
			return positionAnalysisMapper.queryThemeByDate(positionVO);
		} else {
			return positionAnalysisMapper.queryListByDate(positionVO);
		}
	}
	public List<PositionAnalysis> getPitBarByCategory(PositionAnalysis positionVO) {
		if (StringUtils.isEmpty(positionVO.getPlatform())) {
			return positionAnalysisMapper.queryTheBarByCategory(positionVO);
		} else {
			return positionAnalysisMapper.queryPitBarByCategory(positionVO);
		}
	}

	public PositionAnalysis getPageNumByCategory(PositionAnalysis positionVO) {
		if (StringUtils.isEmpty(positionVO.getPlatform())) {
			return positionAnalysisMapper
					.queryThemePageNumByCategory(positionVO);
		} else {
			return positionAnalysisMapper.queryPageNumByCategory(positionVO);
		}
	}

	/**
	 * 查询专题漏斗
	 * @param query 查询条件
	 * @return 结果集
	 */
	@Override
	public List<SpecialTopicFunnelData> getTopicFunnel(TopicFunnelDataQuery query) {
        List<SpecialTopicFunnelData> dataList = new ArrayList<>();
		try {
			TopicFunnelVO topicFunnelVO = positionAnalysisMapper.queryTopicFunnel(query);
			if(null != topicFunnelVO){
				for (TopicPVTypeEnum en : TopicPVTypeEnum.values()){
					SpecialTopicFunnelData funnelData = new SpecialTopicFunnelData();
					funnelData.setName(en.getName());
					if(TopicPVTypeEnum.TOPIC_PAGE_SUM.getCode().equals(en.getCode())){
						funnelData.setValue(topicFunnelVO.getTopicPageSum());
					}else if(TopicPVTypeEnum.DETAIL_PAGE_SUM.getCode().equals(en.getCode())){
						funnelData.setValue(topicFunnelVO.getDetailPageSum());
					}else if (TopicPVTypeEnum.FILL_ORDER_PAGE_SUM.getCode().equals(en.getCode())){
						funnelData.setValue(topicFunnelVO.getFillOrderPageSum());
					}else if(TopicPVTypeEnum.PENDING_PAYMENT_PAGE_SUM.getCode().equals(en.getCode())){
						funnelData.setValue(topicFunnelVO.getPendingPaymentPageSum());
					}else if(TopicPVTypeEnum.COMPLETED_PAYMENT_PAGE_SUM.getCode().equals(en.getCode())){
						funnelData.setValue(topicFunnelVO.getCompletedPaymentPageSum());
					}
					dataList.add(funnelData);
				}
			}
		} catch (Exception e) {
			return null;
		}
		return dataList;
	}

}
